﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace PuzzleCommon
{
    public static class MethodExtenders
    {

        public static void WriteLineCSHeader(this StreamWriter writer, HeaderProtocol header)
        {
            writer.WriteLine(header);
            writer.Flush();
        }

        public static void WriteObject(this StreamWriter writer, object obj)
        {
            BinaryFormatter formatter = new BinaryFormatter();
            formatter.Serialize(writer.BaseStream, obj);
            writer.BaseStream.Flush();
            writer.Flush();
        }

        public static object ReadObject(this StreamReader reader)
        {
            BinaryFormatter formatter = new BinaryFormatter();
            object obj = formatter.Deserialize(reader.BaseStream);
            return obj;
        }

        public static HeaderProtocol ReadLineCSHeader(this StreamReader reader)
        {
            HeaderProtocol header = HeaderProtocol.None;
            string headerString = reader.ReadLine();
            return (HeaderProtocol)Enum.Parse(header.GetType(), headerString);
        }

    }
}
